## Author:	lukas.stoetzer@uzh.ch
## Project: Forecasting Coalition Options
## Content:
##    * Takes results from fit_md_ger.R as an input
##    * Post-prepare output for graphs and tables


## Libaries

library(tidyverse)
library(reshape2)
library(texreg)
library(xtable)


## Load Data

  # Results from 
  res <- list()
  for(e in c(1994,1998,2002,2005,2009,2013,2017)) {
    res[[paste(e)]] <- readRDS(file=paste("ddm_res_",e,".RDS",sep=""))
  }
  

## Analysis 1: Filtered within the last year prior to election

  # Prepare Data: Filtered Values
  df_filter <- cbind(res[["1994"]][[3]],election="1994")
  df_filter$time_rev <- max(df_filter$time) - df_filter$time   + 1
  
  for(e in c(1998,2002,2005,2009,2013, 2017)) {
    tmp_df <- cbind(res[[paste(e)]][[3]],election=paste(e))
    tmp_df$time_rev <- max(tmp_df$time) - tmp_df$time   + 1
    df_filter <- rbind(df_filter,tmp_df)
  }
  
  levels(df_filter$pty_nam) <- c("CDU/CSU","SPD","FDP","DieLinke","Greens","OTHER", "AFD")
  
  # Prepare Data: Polls
  Vtmp <- res[["1994"]][[5]]
  Vtmp_share <- Vtmp/apply(Vtmp,1,sum)
  df_polls <- data.frame(cbind(Vtmp_share[,1:5],NA,Vtmp_share[,6],election="1994",N=c(apply(Vtmp,1,sum))))
  df_polls$time_rev <- nrow(df_polls):1 
  colnames(df_polls) <- c("cdu","spd","fdp","lin","gru","afd","oth","election","N","time_rev")
  
  for(e in c(1998,2002,2005,2009,2013,2017)) {
    Vtmp <- res[[paste(e)]][[5]]
    if(e == "2017"){
      tmp_polls <- data.frame(cbind(Vtmp/apply(Vtmp,1,sum),election=paste(e),N=c(apply(Vtmp,1,sum))))
    } else {
      Vtmp_share <- Vtmp/apply(Vtmp,1,sum)
      tmp_polls <- data.frame(cbind(Vtmp_share[,1:5],NA,Vtmp_share[,6],election=paste(e),N=c(apply(Vtmp,1,sum))))
    }
    tmp_polls$time_rev <- nrow(tmp_polls):1
    colnames(tmp_polls)  <- c("cdu","spd","fdp","lin","gru","afd","oth","election","N","time_rev")
    df_polls <- rbind(df_polls,tmp_polls)
  }
  
  
  # Plot
  clrs <- c("#000000","#F0001C", "#FFED00", "#AA0000", "#58AB27","royalblue","#B3B3B3")
  
  b <- 52 + c(-52,-9*4.5,-6*4.5,-4*3.5,-1)
  b_lab <- c("1 Year", "9 Month","6 Month", "3 Month",  "1 Week")
  
  df_lastyear <- filter(df_filter, time_rev<52) %>% mutate(time_plot = 52 - time_rev)
  df_poll_lastyear <- filter(df_polls, time_rev<52)  %>% select(-N)
  df_poll_lastyear <- melt(df_poll_lastyear, id.vars = c("election","time_rev"))
  names(df_poll_lastyear)[3] <- "pty_nam"
  levels(df_poll_lastyear$pty_nam) <- c("CDU/CSU","SPD","FDP","DieLinke","Greens","AFD","OTHER")
  
  df_poll_lastyear$time_plot <-  52 - df_poll_lastyear$time_rev
  saveRDS(df_poll_lastyear,"df_poll_lastyear.RDS") # Df plot for other plots
  
  
  plot_filter <- ggplot(filter(df_lastyear,pty_nam!="OTHER")) +
    geom_hline(yintercept=0.05,alpha=0.2) +
    geom_point(data=filter(na.omit(df_poll_lastyear),pty_nam!="OTHER"),
               aes(y=as.numeric(value),x=c(time_plot), col = pty_nam),alpha = 0.3, size=0.4) +
    geom_ribbon(aes(ymin=ci5,ymax=ci95,x=time_plot,fill=pty_nam), alpha=0.5) +
    geom_line(aes(x=time_plot,y=mean, group=as.factor(pty_nam), linetype=pty_nam), alpha=0.5) +
    theme_bw() + labs(col="", linetype="",fill="") +
    ylab("Support") + xlab("Time until Election")  +
    scale_x_continuous(breaks =   b, labels=b_lab) + 
    facet_wrap(~election)
  
  
  plot_filter +
    scale_color_grey() +
    scale_fill_grey() 
  
  ggsave(filename = "ddm_filter_german94-17_bw.pdf",width = 12, height = 9)
  
  plot_filter +
    scale_fill_manual(values = clrs) +
    scale_color_manual(values = clrs) 
    
    ggsave(filename = "ddm_filter_german94-17.pdf",width = 12, height = 9)
  

## Analysis 2: Probability of Majority for CDU-FDP and SPD-Greens
  
  # Prepare Data
  df_coal <- cbind(data.frame(res[["1994"]][[4]]),election="1994")
  df_coal$time <- nrow(df_coal):1
  
  for(e in c(1998,2002,2005,2009,2013)) {
    tmp_coal <- cbind(data.frame(res[[paste(e)]][[4]]),election=paste(e))
    tmp_coal$time <- nrow(tmp_coal):1
    df_coal <- rbind(df_coal,tmp_coal)
  }
  
  df_coal <- melt(df_coal,id=c("election","time"))
  names(df_coal) <- c("election","week","coalition","prob")
  df_coal$coalition <- as.factor(df_coal$coalition)
  levels(df_coal$coalition) <- c("CDU/CSU-SPD","CDU/CSU-FDP","CDU/CSU-Greens","CDU/CSU-FDP-Greens","SPD-Greens","SPD-Greens-DieLinke","SPD-Greens-FDP")
  
  # Plot
  df_coal_oy <- filter(df_coal, week<52) %>% mutate(time_plot = 52 - week)
  df_coal_oy_plot <- filter(df_coal_oy, coalition %in% c("CDU/CSU-FDP","SPD-Greens"))

  b <- 52 + c(-52,-9*4.5,-6*4.5,-4*3.5,-1)
  b_lab <- c("1 Year", "9 Month","6 Month", "3 Month",  "1 Week")
  
  ggplot(df_coal_oy_plot,
         aes(x=as.numeric(time_plot),y=prob, group=coalition,linetype=coalition, col=coalition)) +
    geom_smooth(method="loess",span = 0.1, se = F) +
    geom_point(alpha=0.7, size=0.7) +
    facet_wrap(~coalition) +
    theme_bw() + labs(linetype="",col="") +
    xlab("") + ylab("Probability Majority") + xlab("Time until Election")  +
    scale_x_continuous(breaks =   b, labels=b_lab) + 
    facet_wrap(~election) + 
    theme(legend.position="top") +
    scale_colour_grey(start = 0.1,end=0.4)
  
  ggsave(filename = "ddm_coalition_german94-17.pdf",width = 12, height = 6)


## Analysis 3: Average Probabilities

  # Data Frame wirh Majority margin
  
  df_majority <- matrix(c(
    
    "2005","CDU/CSU-SPD","yes",0.81,
    "2005","CDU/CSU-FDP","yes",0.51,
    "2005","CDU/CSU-Greens","yes",0.51,
    "2005","CDU/CSU-FDP-Greens","yes",0.58,
    "2005","SPD-Greens","no",0.44,
    "2005","SPD-Greens-DieLinke","no",0.49,
    "2005","SPD-Greens-FDP","yes",0.52,
    
    "1998","CDU/CSU-SPD","yes",0.81,
    "1998","CDU/CSU-FDP","no",0.43,
    "1998","CDU/CSU-Greens","no",0.44,
    "1998","CDU/CSU-FDP-Greens","yes",0.51,
    "1998","SPD-Greens","yes",0.52,
    "1998","SPD-Greens-DieLinke","yes",0.56,
    "1998","SPD-Greens-FDP","yes",0.58,
    
    "2002","CDU/CSU-SPD","yes",0.83,
    "2002","CDU/CSU-FDP","no",0.49,
    "2002","CDU/CSU-Greens","yes",0.5,
    "2002","CDU/CSU-FDP-Greens","yes",0.58,
    "2002","SPD-Greens","yes",0.51,
    "2002","SPD-Greens-DieLinke","yes",0.51,
    "2002","SPD-Greens-FDP","yes",0.59,
    
    "2005","CDU/CSU-SPD","yes",0.73,
    "2005","CDU/CSU-FDP","no",0.47,
    "2005","CDU/CSU-Greens","no",0.45,
    "2005","CDU/CSU-FDP-Greens","yes",0.55,
    "2005","SPD-Greens","no",0.45,
    "2005","SPD-Greens-DieLinke","yes",0.53,
    "2005","SPD-Greens-FDP","yes",0.54,
      
    "2009","CDU/CSU-SPD","yes",0.62,
    "2009","CDU/CSU-FDP","yes",0.53,
    "2009","CDU/CSU-Greens","no",0.49,
    "2009","CDU/CSU-FDP-Greens","yes",0.64,
    "2009","SPD-Greens","no",0.34,
    "2009","SPD-Greens-DieLinke","no",0.47,
    "2009","SPD-Greens-FDP","yes",0.49,
    
    "2013","CDU/CSU-SPD","yes",0.79,
    "2013","CDU/CSU-FDP","no",0.49,
    "2013","CDU/CSU-Greens","yes",0.59,
    "2013","CDU/CSU-FDP-Greens","no",0.59,
    "2013","SPD-Greens","no",0.41,
    "2013","SPD-Greens-DieLinke","yes",0.51,
    "2013","SPD-Greens-FDP","no",0.41
    
  ),ncol=4,byrow=T)

  
  
  # Prepare as Data.frame
  df_majority <- as.data.frame(df_majority)
  colnames(df_majority) <- c("election","coalition","majority","seat_share")
  df_majority$seat_share <- as.numeric(as.character(df_majority$seat_share))
  
  df <- merge(df_majority,df_coal_oy,by=c("election","coalition"))
  df$seat_majority <- as.numeric(as.character(df$seat_share)) - 0.5


  # Plot Averages
  df$seat_share <- as.numeric(as.character(df$seat_share)) 
  df$seat_share_cut <- cut(df$seat_share,c(0,0.4,0.45,0.5,0.55,0.6,1))
  levels(df$seat_share_cut) <- c("Seat Share <40%", "Seat Share 40%-45%", "Seat Share 45%-50%", "Seat Share 50%-55%","Seat Share 55%-60%","Seat Share >60%")
  
  ggplot((df)) + 
    geom_smooth(aes(x=time_plot,y=prob),method="glm",formula=y ~ poly(x, 2)) + 
    # geom_point(aes(x=time_plot,y=prob,col=seat_share_cut),size=0.4) +
    scale_x_continuous(breaks =   b, labels=b_lab) + 
    theme_bw() + ylab(c(0,1)) + facet_grid( ~ seat_share_cut) +
    scale_colour_grey(start = 0.1,end=0.8) +
    labs(x="Time until Election", y="Probability for Majority",
         col="Seat Share",linetype="Seat Share") 

  ggsave(filename = "ddm_coalition_average_ger94-17.pdf",width = 16, height = 9)
  

  # Linear Models
    
  df$week_to_election <- df$week/52
  df$seat_share_cut2 <- cut(df$seat_share,c(0,0.45,0.5,0.55,1))
  levels(df$seat_share_cut2) <- c("Seat Share < 45%", "Seat Share 45%-50%", "Seat Share 50%-55%","Seat Share > 55%")

  m2 <- lm(prob ~ -1 + coalition*election + seat_share_cut +  seat_share_cut*poly(week_to_election,1,raw=T),data=df)
  m3 <- lm(prob ~ -1 + coalition*election + seat_share_cut +  seat_share_cut*poly(week_to_election,2,raw=T),data=df)

  
  texreg(list(m2,m3), file = "tab1_appendix.tex",
            custom.coef.map= list(
              "seat_share_cutSeat Share <40%:poly(week_to_election, 1, raw = T)" = "Seat Share $<$40\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 40%-45%:poly(week_to_election, 1, raw = T)" = "Seat Share 40\\%-45\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 45%-50%:poly(week_to_election, 1, raw = T)" = "Seat Share 45\\%-50\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 50%-55%:poly(week_to_election, 1, raw = T)" = "Seat Share 50\\%-55\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 55%-60%:poly(week_to_election, 1, raw = T)" = "Seat Share 55\\%-60\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share >60%:poly(week_to_election, 1, raw = T)" = "Seat Share $>$60\\% $\\times$ Weeks to election",
              
              "seat_share_cutSeat Share <40%:poly(week_to_election, 2, raw = T)1" = "Seat Share $<$40\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 40%-45%:poly(week_to_election, 2, raw = T)1" = "Seat Share 40\\%-45\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 45%-50%:poly(week_to_election, 2, raw = T)1" = "Seat Share 45\\%-50\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 50%-55%:poly(week_to_election, 2, raw = T)1" = "Seat Share 50\\%-55\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share 55%-60%:poly(week_to_election, 2, raw = T)1" = "Seat Share 55\\%-60\\% $\\times$ Weeks to election",
              "seat_share_cutSeat Share >60%:poly(week_to_election, 2, raw = T)1" = "Seat Share $>$60\\% $\\times$ Weeks to election", 

              "seat_share_cutSeat Share <40%:poly(week_to_election, 2, raw = T)2" = "Seat Share $<$40\\% $\\times$ Weeks to election squared",
              "seat_share_cutSeat Share 40%-45%:poly(week_to_election, 2, raw = T)2" = "Seat Share 40\\%-45\\% $\\times$ Weeks to election squared",
              "seat_share_cutSeat Share 45%-50%:poly(week_to_election, 2, raw = T)2" = "Seat Share 45\\%-50\\% $\\times$ Weeks to election squared",
              "seat_share_cutSeat Share 50%-55%:poly(week_to_election, 2, raw = T)2" = "Seat Share 50\\%-55\\% $\\times$ Weeks to election squared",
              "seat_share_cutSeat Share 55%-60%:poly(week_to_election, 2, raw = T)2" = "Seat Share 55\\%-60\\% $\\times$ Weeks to election squared",
              "seat_share_cutSeat Share >60%:poly(week_to_election, 2, raw = T)2" = "Seat Share $>$60\\% $\\times$ Weeks to election squared", 
              
              "seat_share_cutSeat Share <40%" = "Seat Share $<$40\\%",
              "seat_share_cutSeat Share 40%-45% " = "Seat Share $40\\% - 45\\%$",
              "seat_share_cutSeat Share 45%-50%" = "Seat Share $45\\% - 50\\%$ ",
              "seat_share_cutSeat Share 50%-55%" = "Seat Share $50\\% - 55\\%$ ",
              "seat_share_cutSeat Share 55%-60%" = "Seat Share $55\\% - 60\\%$ ",
              "seat_share_cutSeat Share >60%"  = "Seat Share $>$60\\% ",
              
              "poly(week_to_election, 1, raw = T)" = "Weeks to election",
              "poly(week_to_election, 2, raw = T)1" = "Weeks to election",
              "poly(week_to_election, 2, raw = T)2" = "Weeks to election squared"
              
            ),
          caption = "Evolution of coalition options. Results from OLS regression with coalition majority probabilities as the dependent variable.",
         custom.note = "%stars. The model includes coalition-election fixed effects that are not reported in the table.",
         label = "tab:app_reg",
            omit.coef=c("coalition|election20"))      
  
  
## Analysis 4: Comparsion to Poll Results 
  

  # Transform to shares
  sel <- c("cdu",  "spd",  "fdp",  "lin",  "gru",  "oth")
  df_polls[,sel] <- apply(df_polls[,sel],2,as.numeric)


  # Create share for coalitions
  
  df_polls$cdu_spd_share <- apply(df_polls[,c("cdu",  "spd")],1,sum)
  df_polls$cdu_fpd_share <- apply(df_polls[,c("cdu",  "fdp")],1,sum)
  df_polls$cdu_gru_share <- apply(df_polls[,c("cdu",  "gru")],1,sum)
  df_polls$cdu_spd_fdp_share <- apply(df_polls[,c("cdu",  "gru", "fdp")],1,sum)
  df_polls$spd_gru_share <- apply(df_polls[,c("spd",  "gru")],1,sum)
  df_polls$spd_gru_lin_share <- apply(df_polls[,c("spd",  "gru", "lin")],1,sum)
  df_polls$spd_gru_fdp_share <- apply(df_polls[,c("spd",  "gru", "fdp")],1,sum)
  
  df_poll_fm <- df_polls[,c(-1,-2,-3,-4,-5,-6,-7)]
  df_poll_fm <- filter(df_poll_fm, time_rev<52) 
  
  df_poll_fm <- melt(df_poll_fm, id.vars = c("election","time_rev","N"))
  names(df_poll_fm) <- c("election","week","N","coalition","polls_share")
  levels(df_poll_fm$coalition) <- c("CDU/CSU-SPD","CDU/CSU-FDP","CDU/CSU-Greens","CDU/CSU-FDP-Greens","SPD-Greens","SPD-Greens-DieLinke","SPD-Greens-FDP")
  df_poll_fm$N <- as.numeric(as.character(df_poll_fm$N))
  df_merge <- merge(df,df_poll_fm)

  df_merge$sd <- sqrt((df_merge$polls_share)*(1- df_merge$polls_share)/as.numeric(df_merge$N))
  df_merge$polls_share_trans <-  pnorm(df_merge$polls_share,mean=0.5,sd=df_merge$sd, lower.tail = T) 
  
  saveRDS(df_merge,file = "ddm_coalition_prob.RDS")
  
  
  
  # b) Relationship with alterantive measurment

  dfmelt <- melt(df_merge[,c("election","coalition","time_plot","seat_share_cut","seat_share_cut2","majority","prob","polls_share_trans")],
                 id.vars = c("election","coalition","seat_share_cut","seat_share_cut2","majority","time_plot"))
  
  # dfmelt$time_plot_cut <- cut(dfmelt$time_plot,4)
  
  
  levels(dfmelt$variable) <- c("Dyn. Multinomial-Dirichlet","Polls directly")
    
  
  ggplot(dfmelt) + geom_boxplot(aes(x=variable,y=value),outlier.size = 0.1) + 
    theme_bw()  + facet_grid(~seat_share_cut2) + 
    theme_bw() + ylab(c(0,1)) +
    scale_colour_grey(start = 0.1,end=0.8) +
    labs(x="Measurment", y="Average Probability for Majority",
         col="Seat Share",linetype="Seat Share") 

  ggsave(filename = "comparsion_ddmcoal_alterantive.pdf",width = 12, height = 6)
  
    
   # a) Relationship of alterantive measurment with true seat share over time
  ggplot((df_merge)) + 
    geom_smooth(aes(x=time_plot,y=polls_share_trans),method="glm",formula=y ~ poly(x, 2)) + 
    # geom_smooth(aes(x=time_plot,y=prob),method="glm",formula=y ~ poly(x, 2)) + 
    # geom_point(aes(x=time_plot,y=prob,col=seat_share_cut),size=0.4) +
    scale_x_continuous(breaks =   b, labels=b_lab) + 
    theme_bw() + ylab(c(0,1)) + facet_grid( ~ seat_share_cut) +
    scale_colour_grey(start = 0.1,end=0.8) +
    labs(x="Time until Election", y="Probability for Majority",
         col="Seat Share",linetype="Seat Share") 
  
  ggsave(filename = "ddm_coalition_average_ger94-13_alterantive.pdf",width = 12, height = 6)

  
  
## Analysis 5: Filtered values and Coalition options for the ongoing election campaign
  
  # Prepare Data: Filtered Values
  df_filter <- cbind(res[["2017"]][[3]],election="2017")
  df_filter$time_rev <- max(df_filter$time) - df_filter$time    
  levels(df_filter$pty_nam) <- c("CDU/CSU","SPD","FDP","DieLinke","Greens","AFD","OTHER")
  
  # Prepare Data: Polls
  Vtmp <- res[["2017"]][[5]]
  df_polls <- data.frame(cbind(Vtmp/apply(Vtmp,1,sum),election="2017"))
  df_polls$time_rev <- nrow(df_polls):1 
  
  
  # Plot
  clrs <- c("#000000","#F0001C", "#FFED00", "#AA0000", "#58AB27","royalblue","#B3B3B3")
  
  b <- 52 + c(-52,-9*4.5,-6*4.5,-4*3.5,-1)
  b_lab <- c("1 Year", "9 Month","6 Month", "3 Month",  "1 Week")
  
  df_lastyear <- filter(df_filter, time_rev<52) %>% mutate(time_plot = 52 - time_rev)
  df_poll_lastyear <- filter(df_polls, time_rev<52)  
  df_poll_lastyear <- melt(df_poll_lastyear, id.vars = c("election","time_rev"))
  names(df_poll_lastyear)[3] <- "pty_nam"
  levels(df_poll_lastyear$pty_nam) <- c("CDU/CSU","SPD","FDP","DieLinke","Greens","AFD","OTHER")
  df_poll_lastyear$time_plot <-  52 - df_poll_lastyear$time_rev
  
  
  plot_filter <- ggplot(filter(df_lastyear,pty_nam!="oth")) +
    geom_hline(yintercept=0.05,alpha=0.2) +
    geom_point(data=filter(na.omit(df_poll_lastyear),pty_nam!="oth"),
               aes(y=as.numeric(value),x=c(time_plot), col = pty_nam),alpha = 0.3, size=0.4) +
    geom_ribbon(aes(ymin=ci5,ymax=ci95,x=time_plot,fill=pty_nam), alpha=0.5) +
    geom_line(aes(x=time_plot,y=mean, group=as.factor(pty_nam), linetype=pty_nam), alpha=0.5) +
    theme_bw() + labs(col="", linetype="",fill="") +
    ylab("Support") + xlab("Time until Election")  +
    scale_x_continuous(breaks =   b, labels=b_lab, limits=c(1,53)) + 
    theme(legend.justification=c(0.005,0), legend.position=c(0.005,0))


  plot_filter +
    scale_color_grey() +
    scale_fill_grey() 
  
  ggsave(filename = "ddm_filter_german17_bw.pdf",width = 12, height = 10)
  
  plot_filter +
    scale_fill_manual(values = clrs) +
    scale_color_manual(values = clrs) +
    
    ggsave(filename = "ddm_filter_german17.pdf",width = 12, height = 10)  
  
  
  # Prepare Data Coalitions
  df_coal <- cbind(data.frame(res[["2017"]][[4]]),election="2017")
  df_coal$time <- nrow(df_coal):1
  
  
  df_coal <- melt(df_coal,id=c("election","time"))
  names(df_coal) <- c("election","week","coalition","prob")
  df_coal$coalition <- as.factor(df_coal$coalition)
  levels(df_coal$coalition) <- c("CDU/CSU-SPD","CDU/CSU-FDP","CDU/CSU-Greens","CDU/CSU-FDP-Greens","SPD-Greens","SPD-Greens-DieLinke","SPD-Greens-FDP")
  
  # Plot
  df_coal_oy <- filter(df_coal, week<52) %>% mutate(time_plot = 52 - week)
  df_coal_oy_plot <- filter(df_coal_oy)
  
  b <- 51 + c(-52,-9*4.5,-6*4.5,-4*3.5,-1)
  b_lab <- c("1 Year", "9 Month","6 Month", "3 Month",  "1 Week")
  
  ggplot(df_coal_oy_plot,
         aes(x=as.numeric(time_plot),y=prob, group=coalition)) +
    # geom_smooth(method="loess",span = 0.01, se = F) +
    geom_line() +
    geom_point(alpha=0.7, size=0.7) +
    facet_wrap(~coalition) +
    theme_bw() +
    xlab("") + ylab("Probability Majority") + xlab("Time until Election")  +
    scale_x_continuous(breaks =   b, labels=b_lab, limits=c(1,51)) + 
    facet_wrap(~ coalition) + 
    scale_colour_grey(start = 0.1,end=0.4)
  
  ggsave(filename = "ddm_coalition_german17.pdf",width = 12, height = 10)
  
  
## Results 1: Discount-paramter estimates   
    
  xtab <- as.matrix(cbind(unlist(lapply(res, `[[`, 1)),
                          unlist(lapply(res, `[[`, 8))))
  colnames(xtab) <- c("Discount-factor","S.E.")
  
  xtab <- xtable(round(xtab,2))
  print(xtab,file  = "discount_factor.tex")  

